package fileProcessors.implementations;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import latex.LatexTable;
import latex.LatexTableFormat;
import latex.LatexTableLines;
import fileProcessors.FileFormatException;
import fileProcessors.FileProcessor;
import fileReading.TableData;
import fileReading.TableFileReader;

public class LatexTableConverter extends FileProcessor {

	@Override
	public void processFiles(File files[], File currentDirectory, File outputFile) throws FileFormatException {
		for(File currentFile:files){
			TableData data=TableFileReader.readTableFile(currentFile);
			data.data=transpose(data.data);
			int columns=data.data.size();
			ArrayList<String> formatStrings=new ArrayList<String>();
			ArrayList<Integer> exponent=new ArrayList<Integer>();
			for(int i=0; i<columns; i++){
				formatStrings.add("0.000");
				exponent.add(0);
			}
			LatexTableFormat format=new LatexTableFormat(exponent, data.columnCaptions, formatStrings, LatexTableLines.HORIZONTAL, null);
			String table=LatexTable.getTable(data.data, format);
			try {
				BufferedWriter bw=new BufferedWriter(new FileWriter(new File(currentFile.getName().substring(0, currentFile.getName().length()-4)+"_latex.txt")));
				bw.write(table);
				bw.close();
			} catch (IOException e) {
			}
		}
	}

	@Override
	public String getInformation() {
		return "Wandelt alle eingelesenen Dateien in eine Latex-Tabelle um.";
	}

	@Override
	public String getDate() {
		return "28.06.13";
	}
	
	public static List<List<Double>> transpose(List<List<Double>> array){
		List<List<Double>> retList=new ArrayList<List<Double>>();
		for(int i=0; i<array.get(0).size(); i++){
			retList.add(new ArrayList<Double>());
		}
		for(int i=0; i<array.get(0).size(); i++){
			for(int j=0; j<array.size(); j++){
				retList.get(i).add(array.get(j).get(i));
			}
		}
		
		return retList;
	}
	

	private static int getMaxColLength(List<List<Double>> data){
		int max=0;
		for(List<Double> col:data){
			if(col.size()>max){
				max=col.size();
			}
		}
		return max;
	}

}
